package com.qumitech.visualnovel.service.novel;

import com.qumitech.visualnovel.datasource.editor.BookDAOMapper;
import com.qumitech.visualnovel.model.dao.editor.BookDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.config.Config;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.common.UrlConstant;

import com.qumitech.visualnovel.service.account.UserInfoService;

import com.qumitech.visualnovel.model.dto.UserFeedbackQueryDTO;
import com.qumitech.visualnovel.model.dto.UserFeedbackUpdateDTO;

import com.qumitech.visualnovel.model.dao.app.Account;
import com.qumitech.visualnovel.model.dao.app.UserFeedbackDAO;
import com.qumitech.visualnovel.model.dao.console.UserInfo;

import com.qumitech.visualnovel.datasource.app.UserFeedbackDAOMapper;
import com.qumitech.visualnovel.datasource.app.AccountMapper;

@Slf4j
@Service
public class UserFeedbackServiceImpl implements UserFeedbackService {
    @Autowired
    UserInfoService userInfoService;

    @Autowired
    AccountMapper accountMapper;
    @Autowired
    UserFeedbackDAOMapper userFeedbackDAOMapper;
    @Autowired
    BookDAOMapper bookDAOMapper;

    @Override
    public BaseResponse userFeedbackList(UserFeedbackQueryDTO param) {
        List<Long> uidList = accountMapper.getUidListByName(param.getUserName());
        if (param.getUidList() != null) {
            param.getUidList().forEach(uid -> {
                uidList.add(uid);
            });
        }

        int total = userFeedbackDAOMapper.total(param);
        List<UserFeedbackDAO> list = userFeedbackDAOMapper.list(param);

        Set<Long> uidSet = new HashSet<>();
        list.forEach(item -> {
            uidSet.add(item.getUid());
        });
        Map<Long, Account> userInfoMap = new HashMap<>();
        if (uidSet.size() > 0) {
            userInfoMap = accountMapper.getUserInfoMap(uidSet);
        }

        Set<Long> bookIdSet = new HashSet<>();
        list.forEach(item -> {
            bookIdSet.add(item.getBookId());
        });
        Map<Long, BookDAO> bookMap = new HashMap<>();
        if (bookIdSet.size() > 0) {
            bookMap = bookDAOMapper.getBookMapByIds(bookIdSet);
        }
        JSONArray result = new JSONArray();
        for (UserFeedbackDAO item : list) {
            Account account = userInfoMap.get(item.getUid());
            JSONObject jItem = new JSONObject();
            jItem.put("id", item.getId());
            jItem.put("uid", item.getUid());
            jItem.put("userName", account==null ? null : account.getUserName());
            jItem.put("feedbackType", item.getFeedbackType());
            jItem.put("content", item.getContent());
            jItem.put("picture", item.getPicture());
            jItem.put("email", item.getEmail());
            jItem.put("submitAt", item.getCreateTime());
            jItem.put("status", item.getStatus());
            jItem.put("operator", item.getOperator());
            jItem.put("operateAt", item.getOperateTime());
            jItem.put("bookId", item.getBookId());
            jItem.put("bookName", bookMap.get(item.getBookId())==null ? null : bookMap.get(item.getBookId()).getTitle());
            jItem.put("delete", bookMap.get(item.getBookId())==null ? null : bookMap.get(item.getBookId()).getDelete());
            result.add(jItem);
        }

        return new BaseResponse(result, total);
    }

    @Override
    public BaseResponse updateUserFeedback(Long id, UserFeedbackUpdateDTO param) {
        UserInfo uInfo = userInfoService.getCurUserInfo();
        param.setOperator(uInfo.getUsername());
        param.setOperateAt(new Date());

        String url = UrlConstant.SERVER_URL_ADMIN_FEEDBACK + "/" + id;
        String response = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(param), UrlConstant.DefaultEncode, null);
        return BaseResponse.fromServerResponse(response);
    }
}
